package org.jeecg.modules.hc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.hc.entity.HcRider;
import org.jeecg.modules.hc.entity.HcRiderStatistics;
import org.jeecg.modules.hc.entity.HcSiteAddressDetails;
import org.jeecg.modules.hc.enums.HcSalaryStatusEnum;
import org.jeecg.modules.hc.mapper.HcRiderStatisticsMapper;
import org.jeecg.modules.hc.service.IHcRiderService;
import org.jeecg.modules.hc.service.IHcRiderStatisticsService;
import org.jeecg.modules.hc.service.IHcSiteAddressDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description: 骑手统计
 * @Author: jeecg-boot
 * @Date: 2023-01-03
 * @Version: V1.0
 */
@Service
public class HcRiderStatisticsServiceImpl extends ServiceImpl<HcRiderStatisticsMapper, HcRiderStatistics> implements IHcRiderStatisticsService {

    @Resource
    private HcRiderStatisticsMapper hcRiderStatisticsMapper;

    @Autowired
    private IHcRiderService hcRiderService;

    @Autowired
    private IHcSiteAddressDetailsService hcSiteAddressDetailsService;

    public void riderJob() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String timeBegin = null;

        //获取最新一条统计的日期
        HcRiderStatistics riderStatisticsLast = this.getOne(new LambdaQueryWrapper<HcRiderStatistics>()
                .eq(HcRiderStatistics::getSalaryStatus, HcSalaryStatusEnum.YES)
                .orderByDesc(HcRiderStatistics::getStatisticalDate)
                .last(" limit 1 ")
        );

        if (riderStatisticsLast != null) {
            Calendar instance = Calendar.getInstance();
            instance.setTime(riderStatisticsLast.getStatisticalDate());
            instance.add(Calendar.DAY_OF_MONTH, 1);
            timeBegin = simpleDateFormat.format(instance.getTime());
        }

        //根据参数查询骑手配送统计数据
        List<HcRiderStatistics> statisticsList = hcRiderStatisticsMapper.getStatisticsList(timeBegin, null);

        if (!CollectionUtils.isEmpty(statisticsList)) {
            for (HcRiderStatistics hcRiderStatistics : statisticsList) {

                HcRider hcRider = hcRiderService.getById(hcRiderStatistics.getRiderId());

                if (hcRider == null) {
                    continue;
                }

//                HcSiteAddressDetails hcSiteAddressDetails = hcSiteAddressDetailsService.getOne(new QueryWrapper<HcSiteAddressDetails>()
//                        .select("GROUP_CONCAT(ADDRESS_NAME Separator '；') as addressName")
//                        .eq("NAME_RIDER", hcRiderStatistics.getRiderId())
//                );
//
//                if (hcSiteAddressDetails == null) {
//                    continue;
//                }

                HcRiderStatistics one = this.getOne(new LambdaQueryWrapper<HcRiderStatistics>()
                        .eq(HcRiderStatistics::getRiderId, hcRiderStatistics.getRiderId())
                        .eq(HcRiderStatistics::getStatisticalDate, hcRiderStatistics.getStatisticalDate())
                );

                BigDecimal divide = hcRider.getOrderQuantityCommission().divide(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP);
                BigDecimal commission = new BigDecimal(hcRiderStatistics.getOrderQuantity()).multiply(divide);
                BigDecimal wages = hcRider.getBaseSalary().add(commission).add(hcRider.getOtherIncome());

                HcRiderStatistics hcRiderStatisticsSave = new HcRiderStatistics()
                        .setRiderId(hcRiderStatistics.getRiderId())
                        .setFullName(hcRider.getFullName())
                        .setPhone(hcRider.getPhone())
                        .setOrderQuantity(hcRiderStatistics.getOrderQuantity())
                        .setBaseSalary(hcRider.getBaseSalary())
                        .setCommission(commission)
                        .setOtherIncome(hcRider.getOtherIncome())
                        .setShippingDetails("")
                        .setOtherIncomeRemarks(hcRider.getOtherIncomeRemarks())
                        .setWages(wages)
                        .setAccumulatedWages(wages)
                        .setSalaryStatus(HcSalaryStatusEnum.NO)
                        .setStatisticalDate(hcRiderStatistics.getStatisticalDate());

                if (one == null) {
                    this.save(hcRiderStatisticsSave);
                } else {
                    if (one.getSalaryStatus().equals(HcSalaryStatusEnum.NO)) {
                        this.updateById(hcRiderStatisticsSave.setId(one.getId()));
                    }
                }

                HcRiderStatistics statistics = this.getOne(new QueryWrapper<HcRiderStatistics>()
                        .select("sum(WAGES) as 'accumulatedWages'")
                        .eq("RIDER_ID", hcRiderStatisticsSave.getRiderId())
                        .eq("SALARY_STATUS", HcSalaryStatusEnum.NO.getValue())
                        .le("STATISTICAL_DATE", hcRiderStatisticsSave.getStatisticalDate())
                );

                this.updateById(new HcRiderStatistics().setId(hcRiderStatisticsSave.getId()).setAccumulatedWages(statistics.getAccumulatedWages()));
            }
        }
    }
}
